<template>
  <div>
    <section class="todoapp">
      <Header @add-todo="babaAddFn" />
      <Mains :obj="showList" @del-todo="babaDelFn" />
      <Footer
        :arr="list"
        @toggle-todo="babaToggleFn"
        v-if="list.length"
        @clear-todo="babaClearFn"
      />
    </section>
  </div>
</template>

<script>
import "./assets/css/base.css";
import "./assets/css/index.css";

import Header from "./components/Header.vue";
import Mains from "./components/Mians.vue";
import Footer from "./components/Footer.vue";
export default {
    watch:{
        list:{
            deep:true,
            handler(newVal){
                localStorage.setItem("list",JSON.stringify(newVal))
            }
        
        }
    },
  computed: {
    showList() {
      if (this.babaType === "all") {
        return this.list;
      } else if (this.babaType === "no") {
        return this.list.filter((item) => item.isDone === false);
      } else {
        return this.list.filter((item) => item.isDone === true);
      }
    },
  },
  methods: {
    //   添加
    babaAddFn(name) {
      const obj = {
        id: Date.now(),
        name,
        isDone: false,
      };
      this.list.push(obj);
    },
    // 删除
    babaDelFn(i) {
      this.list.splice(i, 1);
    },
    //子组件将 type值与父组件同步
    babaToggleFn(type) {
      this.babaType = type;
    },
    // 清除已完成事项
    babaClearFn() {
      this.list = this.list.filter((item) => item.isDone == false);
    },
  },
  data() {
    return {
      babaType: "all",
      list: 
      JSON.parse(localStorage.getItem("list")) ||
      [
        { id: 100, name: "吃饭", isDone: true },
        { id: 201, name: "睡觉", isDone: false },
        { id: 103, name: "打豆豆", isDone: true },
      ],
    };
  },
  components: {
    Header,
    Mains,
    Footer,
  },
};
</script>

<style>
</style>